﻿using UnityEngine;
using System.Collections;
using UnityStandardAssets._2D;

public class ThisIsTheGame : MonoBehaviour
{
	public GameObject HeadUpDisplayAttached;
	public GameObject digit0;
	public GameObject digit1;
	public GameObject digit2;
	public GameObject lifeDisplay;
	public GameObject gameOverPopup;
	public GameObject gameWonPopup;
	private Sprite[] digitsImages = new Sprite[10];
	private Sprite[] LifeImages = new Sprite[4];
	private int score;
	private int lives = 3;
	private int currentLevel = 0;
	private Vector3 deadplace;
	public AudioClip CoinClip;
	public AudioClip TeleportClip;
	public AudioClip LooseClip;
	public AudioClip GameOverClip;
		private  AudioSource audio;
	
	private Vector3[] resetPositions = {
		new Vector3 (-12.0f, -1.6f, 0.0f),
		new Vector3 (-82.0f, 11.0f, 0.0f),
		new Vector3 (-82.0f, 11.0f, 0.0f),
		new Vector3 (28.0f, -37.0f, 0.0f), //2098
		new Vector3 (-12.0f, -1.6f, 0.0f) //You Win
		
		
	};
	private Vector3 currentResetPosition;
	
	
	private enum GAMESTATE
	{
		STARTING,
		INGAME,
		GAMEOVER
	}
	private GAMESTATE gameState = GAMESTATE.STARTING;
	
	
	// Use this for initialization
	void Start ()
	{
		 audio= GetComponent<AudioSource>();
		score = 0;
		prepareLevel ();
		for (int i=0; i<10; i++) {
			digitsImages [i] = Resources.Load (string.Format ("score{0}", i), typeof(Sprite)) as Sprite;
		}
		for (int i=0; i<4; i++) {
			LifeImages [i] = Resources.Load (string.Format ("{0}life", i), typeof(Sprite)) as Sprite;
		}
		displayScore ();
		displayLife ();
	}
	
	void Update ()
	{
	
		
		if (gameState == GAMESTATE.GAMEOVER) {
			this.transform.position = deadplace;
			if (Input.GetKeyDown (KeyCode.R)) {
				Debug.Log ("Restart user!!!!!!!!!!!!!!!!");
				Destroy (this.transform.parent.gameObject);
				Application.LoadLevel (0);
			}
		}
		Vector3 cameraPosition = this.transform.position;
		cameraPosition.z = HeadUpDisplayAttached.transform.position.z;
		HeadUpDisplayAttached.transform.position = cameraPosition;
	}
	
	void prepareLevel ()
	{
		if (gameState == GAMESTATE.STARTING) {
			(gameOverPopup.GetComponent<Renderer> () as Renderer).enabled = false;
			this.transform.position = resetPositions [currentLevel];
			gameState = GAMESTATE.INGAME;
		
			Debug.LogFormat ("Entering Level {0} at {1}/{2} with {3} lives", currentLevel, resetPositions [currentLevel].x, resetPositions [currentLevel].y, lives);
		}	
	}
	
	void displayScore ()
	{
		digit0.GetComponent<SpriteRenderer> ().sprite = digitsImages [score % 10];
		digit1.GetComponent<SpriteRenderer> ().sprite = digitsImages [(score / 10) % 10];
		digit2.GetComponent<SpriteRenderer> ().sprite = digitsImages [(score / 100) % 10];
	}
		
	void displayLife ()
	{
		lifeDisplay.GetComponent<SpriteRenderer> ().sprite = LifeImages [lives];
	}
	// Update is called once per frame
	
	
	void OnLevelWasLoaded (int level)
	{
		currentLevel = level;
		prepareLevel ();
	
		
	}
	
	void OnTriggerEnter2D (Collider2D  coll)
	{  //COLLECT COINS
		string tag = coll.gameObject.tag;
		Debug.LogFormat ("hitTrig: {0} {1}", tag, coll.gameObject.name);
		
		switch (tag) {
		case "coin":
		
			Destroy (coll.gameObject);
			score++;
		
			audio .clip=CoinClip;
			audio.Play();
			displayScore ();
			break;
		
		case "Finish": //nextLevel
			if (gameState == GAMESTATE.INGAME) {
				gameState = GAMESTATE.STARTING;
			currentLevel++;
				Debug.LogFormat ("Goto Level: {0} ", currentLevel);
				audio .clip=TeleportClip;
				audio.Play();
				displayScore ();
			Application.LoadLevel (currentLevel);
			}
			break;
		case "YouWin":
			this.gameState = GAMESTATE.GAMEOVER;
			
			this.deadplace = this.transform.position;
			(gameWonPopup.GetComponent<Renderer> () as Renderer).enabled = true;
			(this.GetComponent<PlatformerCharacter2D> () as PlatformerCharacter2D).moveAllowed = false;
			break;
		}
	}

	void OnCollisionEnter2D (Collision2D coll)
	{
	 
		string tag = coll.gameObject.tag;
		
		switch (tag) {
		case "deathzone":
		case "enemy":
			Debug.Log (coll.gameObject.name);
			if (gameState == GAMESTATE.INGAME) {
				lives--;
				audio.clip=LooseClip;
				audio.Play();
				if (lives >= 0) {
					Debug.Log ("LostLife");
					gameState = GAMESTATE.STARTING;
					displayLife ();
					prepareLevel ();
				} else {
			
					this.gameState = GAMESTATE.GAMEOVER;
					audio.clip=GameOverClip;
					audio.Play();
					this.deadplace = this.transform.position;
					Debug.Log ("gameOver");
					(gameOverPopup.GetComponent<Renderer> () as Renderer).enabled = true;
					(this.GetComponent<PlatformerCharacter2D> () as PlatformerCharacter2D).moveAllowed = false;
				}
			}
			break;
		
		
	
		
		}
	
	}
}
